import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';

class RefreshPage extends StatefulWidget {
  RefreshPage({Key? key}) : super(key: key);

  @override
  _RefreshPageState createState() => _RefreshPageState();
}

class _RefreshPageState extends State<RefreshPage> {
  final RefreshPageController vm = Get.put(RefreshPageController());
  List<String> addStr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
  List<String> str = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];

  /// APPBar淡出效果
  /// 声明全局变量
  late ScrollController scrollController; //scrollView的控制器
  late Positioned rectLeftBtn; //方形返回按钮

  /// 初始化全局变量
  @override
  void initState() {
    super.initState();
    scrollController = ScrollController();
  }

  /// key值，保证页面能够正常刷新
  final GlobalKey<_RefreshPageState> _easyRefreshKey =
      GlobalKey<_RefreshPageState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          /// 监听
          NotificationListener(
            onNotification: (Object? notification) {
              if (notification is ScrollUpdateNotification &&
                  notification.depth == 0) {
                ///滑动通知
                scrollViewDidScrolled(notification.metrics.pixels);
              }

              ///通知不再上传
              return true;
            },
            child: EasyRefresh(
              key: _easyRefreshKey,

              /// 下拉刷新效果
              header: PhoenixHeader(),
              onRefresh: () async {
                print('下拉请求新数据');
              },
              // onLoad: () async {
              //   //上拉请求新数据
              //   print('上拉请求新数据');
              // },
              /// 用于显示数据
              child: CustomScrollView(
                controller: scrollController,
                slivers: <Widget>[
                  // 用SliverPadding包裹以给SliverGrid添加补白。SliverGrid是一个两列，宽高比为4的网格，它有20个子组件。
                  SliverPadding(
                    padding: const EdgeInsets.all(8.0),
                    sliver: SliverGrid(
                      gridDelegate:
                          const SliverGridDelegateWithFixedCrossAxisCount(
                        // Grid按两列显示
                        crossAxisCount: 2,
                        mainAxisSpacing: 10.0,
                        crossAxisSpacing: 10.0,
                        childAspectRatio: 4.0,
                      ),
                      delegate: SliverChildBuilderDelegate(
                        (BuildContext context, int index) {
                          // 创建子widget
                          return Container(
                            alignment: Alignment.center,
                            color: Colors.cyan[100 * (index % 9)],
                            child: Text('grid item $index'),
                          );
                        },
                        childCount: 20,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  void scrollViewDidScrolled(double pixels) {}
}

class RefreshPageController extends GetxController {}
